package com.voto.sunflower.activity.me;

import retrofit.RetrofitError;
import retrofit.client.Response;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.voto.sunflower.R;
import com.voto.sunflower.activity.BaseActivity;
import com.voto.sunflower.model.request.FeedBack;
import com.voto.sunflower.model.response.ResultResponse;
import com.voto.sunflower.retrofit.ClientHttpService;
import com.voto.sunflower.retrofit.NetworkHandler;
import com.voto.sunflower.retrofit.RestError;

public class FeedBackActivity extends BaseActivity implements OnClickListener, TextWatcher {
	private EditText editText;
	private CharSequence textcount;
	private final int MAX_NUM = 200;
	private int selectionStart;
	private int selectionEnd;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_me_setting_feed_back);
		initView();
		setEditText();
	}

	/**
	 * 设置有关功能，默认弹出键盘，跟踪字数
	 */
	private void setEditText() {
		editText = (EditText) findViewById(R.id.feed_back_content);
		editText.setFocusable(true);
		editText.setFocusableInTouchMode(true);
		editText.requestFocus();
		// 默认弹出键盘
		 InputMethodManager inputManager =(InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
	     inputManager.showSoftInput(editText, 0);
		// 设置跟踪字数功能
		editText.addTextChangedListener(this);
	}

	// 初始化标题栏
	private void initView() {
		// 设置标题
		TextView actionBarTitle =(TextView) findViewById(R.id.title); 
		TextView actionBarBack =(TextView) findViewById(R.id.back); 
		TextView actionBarOther =(TextView) findViewById(R.id.other); 
		
		actionBarTitle.setText(getString(R.string.me_feed_back));
		actionBarBack.setText(getString(R.string.me_main_setting));
		actionBarBack.setOnClickListener(this);
		actionBarOther.setText(getString(R.string.me_feed_back_submit));
		actionBarOther.setVisibility(View.VISIBLE);
		actionBarOther.setOnClickListener(this);
		
	}

	class FeedBackCallBack extends NetworkHandler<ResultResponse> {

		@Override
		public void failure(RetrofitError error) {
			super.failure(error);
			dismissDialog();
			if (error.getResponse() != null) {
				int statusCode = error.getResponse().getStatus();
				if (statusCode == 500 || statusCode == 501) {
					final RestError body = (RestError) error.getBodyAs(RestError.class);
					showNetworkErrorDialog(body.errorDetails, null);
				} else if (statusCode == 408) {
					showNetworkErrorDialog(getString(R.string.connect_overtime), null);
				} else {
					showNetworkErrorDialog(error.getMessage(), null);
				}
			}
		}

		@Override
		public void success(ResultResponse arg0, Response arg1) {
			super.success(arg0, arg1);
			dismissDialog();
			//提交成功清空edit
			editText.setText("");
			Toast.makeText(FeedBackActivity.this,R.string.me_feed_back_submit_success,
					Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.other:
			String content = editText.getText().toString();
			FeedBack feedback = new FeedBack();
			feedback.setContent(content);
			showBlankWaitDialog();
			ClientHttpService.getOtherService().sendFeedBack(feedback, new FeedBackCallBack());
			break;
		default:
			break;
		}
	}
	
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		textcount = s;		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterTextChanged(Editable s) {
		// 设置已经输入的字数
				TextView num_hint = (TextView) findViewById(R.id.text_num_hint);
				num_hint.setText(s.length() + "/200");

				selectionStart = editText.getSelectionStart();
				selectionEnd = editText.getSelectionEnd();

				// 当超过字数时
				if (textcount.length() > MAX_NUM) {
					s.delete(selectionStart - 1, selectionEnd);
					editText.setText(s);
				}
	}
}
